<?php

class VerifyEmailForm extends CFormModel
{
	public $verify_code;
	public $uid;
	
	public function rules()
	{
		return array(
			array('verify_code, uid'),
		);
	}
	
	public function verify()
	{
		$verifyEmail = VerifyEmail::model()->findByAttributes(array('uid'=> $this->uid));
		if($verifyEmail != null && $verifyEmail->verify_code == $this->verify_code){
			$transaction = Yii::app()->db->beginTransaction();
			try {
				$account = Account::model()->findByPk($this->uid);
				$account->confirm_email = 1;
				if(!$account->save()){
					throw new Exception('Can not save account.');
				}
				if(!$verifyEmail->delete()){
					throw new Exception('Can not delete confirm');
				}
				$transaction->commit();
				return true;
			}
			catch(Exception $e) {
				$transaction->rollback();
			}
		}
		return false;
	}
}